package com.yu.task.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.task.globalResult.R;
import com.yu.task.pojo.Blog;
import com.yu.task.pojo.BlogTagVo;
import com.yu.task.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blog")
@Api(tags = {"博客实体"})
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation("根据id删除博客")
    @PostMapping("/deleteBlogOne")
    public R deleteBlogOne(
            @ApiParam(name = "博客id",value = "博客id长整型",required = true)
            @RequestParam Long id){
        int i = blogService.deleteBlogOne(id);
        return R.success();

    }

    /**
     * 更新
     * @param blog
     * @return
     */
    @PostMapping("/updateOne")
    public R updateOne(@RequestBody BlogTagVo blogTagVo){
        blogTagVo.setUpdateTime(new Date());
        blogTagVo.setCreateTime(new Date());
        return blogService.updateOne(blogTagVo);
    }

    /**
     * 查询所有博客
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getBlogTagVo")
    public R getBlogTagVo(@RequestParam int page,@RequestParam int size){
        System.out.println(StpUtil.getLoginId());
        Page<BlogTagVo> list = blogService.getBlogTagVo(page,size);
        return R.success(list);
    }

    /**
     * 根据用户id分页查询所有博客
     * @param page
     * @param size
     * @param userId
     * @return
     */
    @GetMapping("/getBlogTagVoById")
    public R getBlogTagVoById(@RequestParam int page,@RequestParam int size,@RequestParam Long userId){

        Page<BlogTagVo> list = blogService.getBlogTagVoPageById(page,size,userId);
        return R.success(list);
    }

    /**
     * 生成一条博客
     * @param blogTagVo
     * @return
     */
    @PostMapping("/insertBlogOne")
    public R insertBlogOne(@RequestBody BlogTagVo blogTagVo){
        blogTagVo.setUpdateTime(new Date());
        blogTagVo.setCreateTime(new Date());
        return blogService.insertBlogOne(blogTagVo);
    }




}
